const RightsModel = require("../model/RightsModel")

const RigthsService = {
    getList:async () => {
        return await RightsModel.find()
    },
    updateList:async ({body,title}) => {
        if(body._id){
            return await RightsModel.findOneAndUpdate({_id:body._id},{
                "$set":{
                    title:title
                }
            })
        }else{
            return RightsModel.findByIdAndUpdate({
                "children.path":data.path
            },{
                "$set":{
                    "children.$.title":title
                }
            })
        }
    },
    deleteList:async (body) => {
        if(body._id){
            return RightsModel.findOneAndDelete({"_id":body._id})
        }else{
            return RightsModel.findOneAndUpdate(
            {
                "children.path":body.path
            },
            {
                "$pull":{"children":{path:body.path}}
            }
            )
        }
    }
}
module.exports = RigthsService